﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace School.SystemManage.WebPages.SchoolIntro
{
    public partial class TeacherList : Base.PermissionBasePage
    {
        TechnologySchoolEntities db = new TechnologySchoolEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            var teachers = db.Teachers.OrderBy(t => t.Position).ThenBy(t => t.Major).ThenBy(t => t.TOrder);

            lv_teacher.DataSource = teachers;
            lv_teacher.DataBind();
        }

        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListViewDataItem item in lv_teacher.Items)
                {
                    CheckBox cbx = (CheckBox)item.FindControl("cbChecked");
                    if (cbx.Checked)
                    {
                        int tid = (int)lv_teacher.DataKeys[item.DataItemIndex].Value;
                        var teacher = db.Teachers.FirstOrDefault(t => t.TID == tid);
                        if (teacher != null)
                        {
                            string imgPath = teacher.PhotoPath;

                            db.DeleteObject(teacher);

                            string filePath = Server.MapPath(imgPath);
                            if (File.Exists(filePath))
                            {
                                File.Delete(filePath);
                            }
                        }
                    }
                }
                db.SaveChanges();

                BindData();
            }
            catch (Exception ex)
            {
                lblMsg.Text = "删除过程发生异常，原因：" + ex.Message;
            }
        }

        protected string GetMajor(string code)
        {
            string major = string.Empty;
            switch (code)
            { 
                case "0":
                    major = "会计";
                    break;
                case "1":
                    major = "国贸";
                    break;
                case "2":
                    major = "营销";
                    break;
                case "3":
                    major = "国商";
                    break;
                default:
                    break;
            }
            return major;
        }

        protected void lv_teacher_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            if (e.CancelMode == ListViewCancelMode.CancelingEdit)
            {
                lv_teacher.EditIndex = -1;
                BindData();
            }
        }

        protected void lv_teacher_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lv_teacher.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void lv_teacher_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            TextBox txbOrder = (TextBox)lv_teacher.EditItem.FindControl("txbOrder");
            int keyid = (int)lv_teacher.DataKeys[e.ItemIndex].Value;
            int order = txbOrder.Text == string.Empty ? 0 : int.Parse(txbOrder.Text.Trim());

            Teachers teacher = db.Teachers.FirstOrDefault(t => t.TID == keyid);
            if (teacher != null)
            {
                teacher.TOrder = order;

                if (db.SaveChanges() > 0)
                {
                    lv_teacher.EditIndex= -1;
                    BindData();
                }
                else
                {
                    lblMsg.Text = "修改失败！";
                }
            }           
        }

        protected string GetPositionName(string code)
        {
            string name = string.Empty;
            switch (code)
            { 
                case "0":
                    name = "院系领导";
                    break;
                case "1":
                    name = "院系教师";
                    break;
                case "2":
                    name = "历任领导";
                    break;
                default:
                    break;
            }

            return name;
        }

        #region 操作权限控制

        public override void SetEditDisable()
        {
            base.SetEditDisable();

            btnAdd.Visible = false;
            lbtnDelete.Visible = false;
            foreach (ListViewDataItem item in lv_teacher.Items)
            {
                if (item.ItemType == ListViewItemType.DataItem)
                {
                    LinkButton linkOrder = (LinkButton)item.FindControl("lbtnOrder");
                    linkOrder.Visible = false;
                    HtmlGenericControl linkEdit = (HtmlGenericControl)item.FindControl("linkEditContainer");
                    linkEdit.Visible = false;
                }
            }

        }

        public override void SetReviewDisable()
        {
            base.SetReviewDisable();
        }

        public override void SetViewDisable()
        {
            base.SetViewDisable();
        }

        #endregion
    }
}
